package genedit.controller.interfaceActions;

import genedit.gui.GMainFrame;
import genedit.gui.GProjectTree;
import genedit.model.HierarchyElement;
import genedit.model.Project;
import genedit.model.Workspace;
import genedit.model.graph.GraphDocument;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;
import javax.swing.tree.TreeNode;

/**
 * Akcija čuva dokument na putanju na kojoj je već bio sačuvan.
 * 
 * @author Nikola
 * @version 1.1 5/1/14
 */
public class GSaveA extends AbstractAction {
	private static final long serialVersionUID = 1L;

	private static GSaveA instance = null;

	public static GSaveA getInstance() {
		if (instance == null)
			instance = new GSaveA();
		return instance;
	}

	private GSaveA() {
		putValue(NAME, "Save");
		putValue(SMALL_ICON, new ImageIcon("resources/icons/floppy_disk.png"));
		putValue(MNEMONIC_KEY, KeyEvent.VK_N);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		putValue(SHORT_DESCRIPTION, "Save");
	}

	/**
	 * Akcija čuva projekat na putanji na kojoj je već bio sačuvan. Ukoliko ta
	 * putanja ne postoji poziva se Save As kako bi korisnik izabrao putanju i
	 * format u kom želi da čuva projekat.
	 */
	public void actionPerformed(ActionEvent arg0) {
		TreeNode element = null;
		if (GProjectTree.getInstance().getTree().hasFocus()) {
			element = (TreeNode) GProjectTree.getInstance().getTree()
					.getLastSelectedPathComponent();
			if (element instanceof GraphDocument) {
				GMainFrame.getInstance().documentSaved((GraphDocument) element);
			}
		} else if (Workspace.getInstance().getActiveDocument() != null) {
			element = (TreeNode) Workspace.getInstance().getActiveDocument().getModel();
			GMainFrame.getInstance().documentSaved((GraphDocument) element);
		} else {
			return;
		}

		if (element instanceof Workspace)
			return;

		while (!(element instanceof Project))
			element = (HierarchyElement) element.getParent();

		Project project = (Project) element;
		if (project.getPath() == null) {
			GSaveAsA.getInstance().actionPerformed(arg0);
			return;
		}

		try {
			if (project.getPath().contains(".xml")) {
				BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream(project.getPath()), "UTF8"));
				Workspace.getInstance().getXstream().toXML(project, out);
			} else {
				ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
						project.getPath()));
				out.writeObject(project);
				out.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}